package Frame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

import com.hankcs.hanlp.HanLP;

import CheckFile.CheckFile;
import Rank.insertIntoCustomDictionary;
import Rank.outPutRankText;
import Rule.FileChooserFilter;
import Rule.NameTag;
import Rule.chartoDBC;
import Table.MyTable;

public class ExcelConvertData extends JPanel{
	
    static JScrollPane topScrollPane;
	final JScrollPane bottomScrollPane;
	private static JTextField tfFilePath;
	private static JButton button_Show;
	private static JButton button_Convert;
	
	public ExcelConvertData(){
		
		topScrollPane=new JScrollPane(); 
		topScrollPane.setBorder(BorderFactory.createTitledBorder(NameTag.common_Tittle_2));
		bottomScrollPane=new JScrollPane();
		bottomScrollPane.setBorder(BorderFactory.createTitledBorder(NameTag.common_Tittle_2));	
		JSplitPane Splitpane=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		Splitpane.setResizeWeight(0.4);//分割面板
		Splitpane.setLeftComponent(topScrollPane);
		Splitpane.setRightComponent(bottomScrollPane);
		JPanel panel_1 = new JPanel();
		JLabel label_1 = new JLabel(NameTag.common_File);
		tfFilePath = new JTextField("",20);
		tfFilePath.setEditable(false);		
		button_Show = new JButton(NameTag.show_1);
		button_Show.addActionListener(new ButtonListener());
		button_Convert = new JButton(NameTag.convert_1);
		button_Convert.addActionListener(new ButtonListener());

		panel_1.add(label_1);
		panel_1.add(tfFilePath);
		panel_1.add(button_Show);
		panel_1.add(button_Convert);
		this.setLayout(new BorderLayout());	
		this.add(panel_1, BorderLayout.NORTH);
		this.add(topScrollPane, BorderLayout.CENTER);		
	}
	
	
	private static class ButtonListener implements ActionListener{
			
		private static  JTable mytable;
		@Override
		public void actionPerformed(ActionEvent e) {
			
				if(e.getSource() == button_Show ){
					
					String sFilePath = FileChooserFilter.ExcelFilter(tfFilePath);
					if(sFilePath == null){return;}
					mytable = MyTable.getTable(sFilePath);	
					topScrollPane.setViewportView(mytable);				
				}	
				else if(e.getSource() == button_Convert){
					
					if(mytable == null){return;}
					CheckFile.CheckHanLPFile(); //检查HanLP配置文件
					insertIntoCustomDictionary.sqlDataInsertIntoCustomDicionary(); //加载自定义词典				
					ArrayList<String> textList = new ArrayList();
					outPutRankText outprt = new outPutRankText(mytable);
					textList = outprt.getTextList();
					for(String s:textList){
						
						
						
						String s1 = HanLP.segment(s).toString();
						System.out.println(s1);
						if(s1.contains("/m") && s1.contains("/q")) //判断个数
						{
							s1 = s1.replace("/m", "?");
							System.out.println(s1);
						}
						
						String s2[] = s1.split(",");
						for(int x = 0 ; x < s2.length; x++) 
						{ 
							String s3 = s2[x];
							if( s3.contains("/g")  || s3.contains("/q"))
							{
								if(s3.contains("["))
								{
									s3 = s3.replace("[", "");
								}
								if(s3.contains("]"))
								{
									s3 = s3.replace("]", "");
								}							
								//System.out.print(s3.trim());
							}	
							
						}
						System.out.println();
					}
					mytable = null;
					topScrollPane.updateUI();
					tfFilePath.setText("");

				}
		}
	
	}
		
}
